// 推广

/**
 * 传入对应的节点 返现最新的数据
 * @param {string} income 收益
 * @param {string} promotion 推广
 * @param {string} deal 成交
 */
const promotionShowData = (income = 'income', promotion = "promotion", deal = 'deal') => {
  const incomeEl = document.getElementById(income);
  const promotionEL = document.getElementById(promotion);
  const dealEl = document.getElementById(deal);

  const incomeList = cache.getItem('incomeList') || []
  const userInfo = cache.getItem('userInfo')

  const incomeInfo = incomeList.find(item => +userInfo?.id === +item.userId)
  if (incomeInfo) {
    console.log(incomeInfo)
    promotionEL.innerText = incomeInfo?.promotion || 0
    dealEl.innerText = incomeInfo?.deal || 0
    incomeEl.innerText = incomeInfo?.income || 0
  } else {
    promotionEL.innerText = 0
    dealEl.innerText = 0
    incomeEl.innerText = 0
  }
}

/**
 * 推广
 * @param {object} item 商品信息 起码包含 id name price cover
 * @param {number} income 佣金价格
 * @returns 
 */
const promotion = (item = {}, income) => {
  const userInfo = cache.getItem('userInfo')
  if (!userInfo) return Message('请先登录！')
  if (!item.id) return Message('请传入商品的信息')
  const incomeList = cache.getItem('incomeList') || []

  // 推广信息
  const incomeInfo = incomeList.find(item => +userInfo.id === +item.userId)

  // 添加
  if (!incomeInfo) {
    incomeList.push({
      userId: userInfo.id,
      user: userInfo.name,
      deal: 0,
      income: 0,
      promotion: 1,
      buyerList: [],
      proList: [
        {
          ...item,
          income
        }
      ]
    })
  } else {
    // 查看商品是否存在
    const isHas = incomeInfo.proList?.find(shop => shop.id === item.id)

    incomeInfo.promotion++
    if (!isHas) {
      incomeInfo.proList.push({
        ...item,
        income
      })
    }
  }

  // 存入
  cache.setItem('incomeList', incomeList)
  promotionShowData()

  copy(`${location.host}${location.pathname}?id=${item.id}&share=${userInfo.name}&income=${income}`)
  // copy(`http://localhost:8080/ssm_zxncpshop/productDetails.do?id=${item.id}&share=${userInfo.name}&income=${income}`)
}

/**
 * 登录
 * @param {object} info 用户信息 
 */
const promotionLogin = (info) => {
  cache.setItem('userInfo', info)
  promotionShowData()
}

// 退出登录
const promotionExit = () => {
  cache.removeItem('userInfo')
  promotionShowData()
}

// 结算
const promotionSettlement = () => {
  const userInfo = cache.getItem('userInfo')
  if (!userInfo) return Message('请先登录！')
  const incomeList = cache.getItem('incomeList') || []

  // 推广信息
  const incomeInfo = incomeList.find(item => +userInfo.id === +item.userId)

  // 获取当前用户的购物车
  const { buyerList } = incomeInfo

  // 根据购物车信息 给对应用户增加 收益 和成交次数
  buyerList.forEach(item => {
    const currentShare = incomeList.find(user => user.user === item.userName)

    if (currentShare) {
      currentShare.income += item.income * item.count
      currentShare.deal++
    }
  })
  incomeInfo.buyerList = []

  // 存入
  cache.setItem('incomeList', incomeList)
  promotionShowData()
}

/**
 * 加入购物车
 * @param {style} type 
 * @returns 
 */
const promotionJoinBuyer = (type) => {
  const userInfo = cache.getItem('userInfo')
  if (!userInfo) return Message('请先登录！')
  const incomeList = cache.getItem('incomeList') || []

  // 推广信息
  const incomeInfo = incomeList.find(item => +userInfo.id === +item.userId)

  const searchParams = new URLSearchParams(location.search)
  // 分享人
  const share = searchParams.get('share')
  // 佣金
  const income = searchParams.get('income')
  const shopId = searchParams.get('id')


  // 如果分享人是当前用户 不计入
  if (share === userInfo.name) return
  // 本地存储不含 添加
  if (!incomeInfo) {
    incomeList.push({
      userId: userInfo.id,
      user: userInfo.name,
      deal: 0,
      income: 0,
      promotion: 0,
      proList: [],
      buyerList: [
        {
          userName: share,
          income,
          count: 1,
          shopId
        }
      ]
    })
  }


  // 是分享的商品
  if (share && incomeInfo) {
    if (!incomeInfo['buyerList']) {
      incomeInfo['buyerList'] = [{
        userName: share,
        income,
        count: 1,
        shopId
      }]
    } else {
      // 查看商品是否在购物车中
      const shopIndex = incomeInfo['buyerList']?.findIndex(item => item.shopId === shopId)
      if (shopIndex !== -1) {
        incomeInfo['buyerList'][shopIndex].count++
      } else {
        incomeInfo['buyerList'].push({
          userName: share,
          income,
          count: 1,
          shopId
        })
      }
    }
  }

  cache.setItem('incomeList', incomeList)
}



const incomeList = [{
  userId: 12, // 用户id
  user: 'xxx', // 用户信息
  deal: 134, // 总成交
  income: 566.99, // 总收益
  promotion: 236, // 总推广次数
  proList: [ // 推广商品列表
    {
      id: 123, // 商品ID
      name: '123', // 商品名称
      price: '123', // 商家价格
      cover: '123', // 商品封面
      income: 4.99 // 引进佣金
    }
  ],
  buyerList: [// 购物车列表
    {
      shopId: 12, // 商品ID,
      userName: 12,  // 推广人姓名
      income: 1.4, // 佣金金额
      count: 10 // 购买数量
    }
  ]
}]

// 存入


// 内容复制
function copy(value) {
  const input = document.createElement('input');
  document.body.appendChild(input);
  input.setAttribute('value', value);
  input.select();
  if (document.execCommand('copy')) {
    document.execCommand('copy');
    Message('该商品的分享链接已复制成功！')
  }
  document.body.removeChild(input);
}
